<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  install
 */

/**
 * To use this script you must configure your crontab like this :
 *
 *      * * * * * wget -q "http://yoursite/cron.php?key=yourSecretKey" > /dev/null
 *
 * Your secret key can be found in the .key file in webapp/config
 */


require_once("..".DIRECTORY_SEPARATOR."codizer".DIRECTORY_SEPARATOR."FrameworkInit.class.php");

if(isset($_GET['key']))
{
    $key = file_get_contents(CONFIG_DIR."/.key");

    if($_GET['key'] == $key)
    {
        $xmlList = f_file_Resolver::getInstance()->fromDirectory(MODULES_DIR)->setRecursive(true)->setMask('config'.DS.'cron.xml')->find();

        if(count($xmlList) > 0)
        {
            foreach($xmlList as $xmlFile)
            {
                $path = explode(DS, $xmlFile);

                $module = $path[count($path) - 3];

                $xml = simplexml_load_file($xmlFile);

                $jobs = $xml->xpath('//job');

                if(count($jobs) > 0)
                {
                    foreach ($jobs as $job)
                    {
                        $time = (string) $job->attributes()->time;
                        
                        if(f_cron_Job::getInstance()->setExecutionTime($time)->mustExecute())
                        {
                            $action = (string) $job->attributes()->action;

                            if(isset($job->attributes()->module))
                            {
                                $module = (string) $job->attributes()->module;
                            }

                            f_core_Controller::getInstance()->setModule($module)->setAction($action)->dispatch();
                        }
                    }
                }

            }
        }
    }
}


